//
//  AddMemberView.m
//  HAODE
//
//  Created by panquanReabam.com on 16/5/20.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "AddMemberView.h"
#import "UIAlertView+show.h"
#import "TagView.h"
@interface AddMemberView()<UITextFieldDelegate,UIGestureRecognizerDelegate,UIScrollViewDelegate,UITextViewDelegate>

@property (nonatomic,weak) id delegate;

@end
@implementation AddMemberView

{
    NSString*isDefault;
    BOOL saveAccount;
    UIImageView* ima;
    UILabel*labelNote;
    TagView*tagV;
    UIView* myToolBar;
}

@synthesize mDelegate;
#pragma mark LifeCycle
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}
#pragma mark InitUI
- (void)initUI
{
    self.backgroundColor=[UIColor whiteColor];
     myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
    myToolBar.backgroundColor=jianpancolor;
    UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
    [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:tubtnDone];
    UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
    [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
    [btnDone111 setTitleColor:jianpancolor1 forState:UIControlStateNormal];
    btnDone111 .titleLabel .font=[UIFont systemFontOfSize:keyboardFot];
    [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:btnDone111];
    myToolBar.alpha=alp;
    UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
    [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:dabtn];

    _myscrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight)];
    [_myscrollView setBackgroundColor: zuiqianhuiColor];
    _myscrollView.delegate = self;
    _myscrollView.userInteractionEnabled = YES;
    _myscrollView.scrollEnabled = YES;
    _myscrollView.showsVerticalScrollIndicator = FALSE;
    _myscrollView.showsHorizontalScrollIndicator = FALSE;
    
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapDone)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [_myscrollView addGestureRecognizer:singleFingerOne];
    
    UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 88/2)];
    view1.backgroundColor=[UIColor whiteColor];
    [_myscrollView addSubview:view1];
    
    UIView*view2=[[UIView alloc]initWithFrame:CGRectMake(0, 88/2+10, ScreenWidth, 88/2*5)];
    view2.backgroundColor=[UIColor whiteColor];
    [_myscrollView addSubview:view2];
    UILabel* duogou=[[UILabel alloc]initWithFrame:CGRectMake(20,0, 40, 44)];
    duogou.text=[NSString stringWithFormat:@"导购"];
    duogou.textColor=zhonghuiColor;
    duogou.font=[UIFont systemFontOfSize:15];
    [_myscrollView addSubview:duogou];
    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* userName = [user1 objectForKey:@"userName"];
//   self.GuideCode = [user1 objectForKey:@"UserCode"];
    UIButton*daogouname=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-200,  0,180,44)];
    [daogouname setTitle:userName forState:UIControlStateNormal];
    daogouname.titleLabel.font=[UIFont systemFontOfSize:15];
    daogouname.titleLabel.textAlignment=NSTextAlignmentLeft;
    daogouname.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [daogouname setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_myscrollView addSubview:daogouname];
    
    UIView*huiseview=[[UIView alloc]initWithFrame:CGRectMake(0,44, ScreenWidth, 10)];
    huiseview.backgroundColor=zuiqianhuiColor;
    huiseview.layer.borderWidth = 0.35;
    huiseview.layer.borderColor = qianhuiColor.CGColor;
    [_myscrollView addSubview:huiseview];
    
    
    UILabel* WeChatId=[[UILabel alloc]initWithFrame:CGRectMake(20, 88/2+10, 70,44)];
    WeChatId.text=[NSString stringWithFormat:@"%@", @"微信昵称"];
    WeChatId.font=[UIFont systemFontOfSize:15];
    WeChatId.textColor=[UIColor blackColor];
    [_myscrollView addSubview:WeChatId];
    
    _weChatIdTextFied=[[UITextField alloc]initWithFrame:CGRectMake(WeChatId.right, 89/2+10, 150, 44)];
    _weChatIdTextFied.borderStyle = UITextBorderStyleNone;
    _weChatIdTextFied.font = [UIFont systemFontOfSize:15];
    _weChatIdTextFied.textColor=[UIColor blackColor];
    _weChatIdTextFied.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    _weChatIdTextFied.tag=1;
    _weChatIdTextFied.placeholder = @"请获取微信昵称"; //默认显示的字
    [_weChatIdTextFied setUserInteractionEnabled:NO];
    _weChatIdTextFied.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_myscrollView addSubview:_weChatIdTextFied];
    
    UIButton* QrCodeBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-100,  88/2+20, 80,erweimaFot)];
    [QrCodeBtn.layer setCornerRadius:2.0];
    [QrCodeBtn.layer setBorderWidth:0.3];
    [QrCodeBtn.layer setMasksToBounds:YES];
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){153/255.0,153/255.0, 153/255.0, 1.0   });
    [QrCodeBtn.layer setBorderColor:colorref2];//边框颜色
    [QrCodeBtn setTitle:@"二维码" forState:UIControlStateNormal];
     QrCodeBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [QrCodeBtn setTitleColor:shenhuiColor forState:UIControlStateNormal];
    [QrCodeBtn addTarget:self action:@selector(QrCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_myscrollView addSubview: QrCodeBtn];
   
    
    UILabel* UserName1=[[UILabel alloc]initWithFrame:CGRectMake(20, 88/2+10+44, 70,88/2)];
    UserName1.text=[NSString stringWithFormat:@"%@", @"姓名"];
    UserName1.font=[UIFont systemFontOfSize:15];
    UserName1.textColor=[UIColor blackColor];
    [_myscrollView addSubview:UserName1];
    _userNameTextFied=[[UITextField alloc]initWithFrame:CGRectMake(WeChatId.right,  88/2+10+44, 150,  88/2)];
    _userNameTextFied.borderStyle = UITextBorderStyleNone;
    _userNameTextFied.font = [UIFont systemFontOfSize: 15];
    _userNameTextFied.textColor=[UIColor blackColor];
    _userNameTextFied.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    _userNameTextFied.tag=2;
    _userNameTextFied.placeholder = @"请输入会员姓名"; //默认显示的字
    _userNameTextFied.delegate = self;
    [_userNameTextFied setUserInteractionEnabled:YES];
    _userNameTextFied.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_myscrollView addSubview:_userNameTextFied];
    
    
   
    UILabel* Phone=[[UILabel alloc]initWithFrame:CGRectMake(20, 44*2+10+44, 70,88/2)];
    Phone.text=[NSString stringWithFormat:@"%@", @"手机号码"];
    Phone.font=[UIFont systemFontOfSize:15];
    Phone.textColor=[UIColor blackColor];
    [_myscrollView addSubview:Phone];
    _phoneTextFied=[[UITextField alloc]initWithFrame:CGRectMake(WeChatId.right,  44*2+10+44, 150,  88/2)];
    _phoneTextFied.borderStyle = UITextBorderStyleNone;
    _phoneTextFied.keyboardType = UIKeyboardTypeNumberPad;
    _phoneTextFied.font = [UIFont systemFontOfSize: 15];
    _phoneTextFied.textColor=[UIColor blackColor];
    _phoneTextFied.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    _phoneTextFied.tag=3;
    _phoneTextFied.placeholder = @"请输入会员手机号码"; //默认显示的字
    _phoneTextFied.delegate = self;
    [_phoneTextFied addTarget:self action:@selector(userNumberLimitTextLength:) forControlEvents:UIControlEventEditingChanged];
    [_phoneTextFied setUserInteractionEnabled:YES];
    _phoneTextFied.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_myscrollView addSubview:_phoneTextFied];
    UILabel* ProvinceName=[[UILabel alloc]initWithFrame:CGRectMake(20, 44*3+10+44, 70,88/2)];
    ProvinceName.text=[NSString stringWithFormat:@"%@", @"地区"];
    ProvinceName.font=[UIFont systemFontOfSize:15];
    ProvinceName.textColor=[UIColor blackColor];
    [_myscrollView addSubview:ProvinceName];
    UIButton*area=[[UIButton alloc]initWithFrame:CGRectMake(ProvinceName.right, 44*3+10+44, ScreenWidth, 88/2)];
    [area addTarget:self action:@selector(area:) forControlEvents:UIControlEventTouchUpInside];
    [_myscrollView addSubview:area];
    UIButton*jiantouBtn3 = [[UIButton alloc]init];
    jiantouBtn3.frame = CGRectMake(ScreenWidth-30,44*3+25+44,8, 13);
    [jiantouBtn3 setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
    [_myscrollView addSubview:jiantouBtn3];
    
    _regionTextField=[[UITextField alloc]initWithFrame:CGRectMake(WeChatId.right,  44*3+10+44, 150, 88/2)];
    _regionTextField.borderStyle = UITextBorderStyleNone;
    _regionTextField.font = [UIFont systemFontOfSize: 15];
    _regionTextField.textColor=[UIColor blackColor];
    _regionTextField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    _regionTextField.tag=4;
    _regionTextField.placeholder = @"请选择地区"; //默认显示的字
    [_regionTextField setUserInteractionEnabled:NO];
    _regionTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_myscrollView addSubview:_regionTextField];
    
    
    
    UILabel* Address=[[UILabel alloc]initWithFrame:CGRectMake(20, 44*4+10+44, 70,88/2)];
    Address.text=[NSString stringWithFormat:@"%@", @"收货地址"];
    Address.font=[UIFont systemFontOfSize:15];
    Address.textColor=[UIColor blackColor];
    [_myscrollView addSubview:Address];
    _addressTextFied=[[UITextField alloc]initWithFrame:CGRectMake(WeChatId.right, 44*4+10+44, 250,  88/2)];
    _addressTextFied.borderStyle = UITextBorderStyleNone;
    _addressTextFied.font = [UIFont systemFontOfSize: 15];
    _addressTextFied.textColor=[UIColor blackColor];
    _addressTextFied.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    _addressTextFied.tag=5;
    _addressTextFied.placeholder = @"请输入详细地址"; //默认显示的字
    [_addressTextFied setUserInteractionEnabled:YES];
    _addressTextFied.delegate = self;
    _addressTextFied.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_myscrollView addSubview:_addressTextFied];
    
    
   
    UILabel *line1 = [[UILabel alloc]initWithFrame:CGRectMake(15,88/2+10+44, ScreenWidth-30, 0.35)];
    line1.backgroundColor = qianhuiColor;
    [_myscrollView addSubview:line1];
    UILabel *line2 = [[UILabel alloc]initWithFrame:CGRectMake(15,88+10+44, ScreenWidth-30, 0.35)];
    line2.backgroundColor = qianhuiColor;
    [_myscrollView addSubview:line2];
    UILabel *line3 = [[UILabel alloc]initWithFrame:CGRectMake(15,88/2*3+10+44, ScreenWidth-30, 0.35)];
    line3.backgroundColor = qianhuiColor;
    [_myscrollView addSubview:line3];
    UILabel *line4 = [[UILabel alloc]initWithFrame:CGRectMake(15,88/2*4+10+44, ScreenWidth-30, 0.35)];
    line4.backgroundColor = qianhuiColor;
    [_myscrollView addSubview:line4];
    UILabel *line5 = [[UILabel alloc]initWithFrame:CGRectMake(15,88/2*5+10+44, ScreenWidth-30, 0.35)];
    line5.backgroundColor = qianhuiColor;
    [_myscrollView addSubview:line5];

    
    
    _addressTextFied.inputAccessoryView = myToolBar;
    _phoneTextFied.inputAccessoryView = myToolBar;
    _userNameTextFied.inputAccessoryView = myToolBar;
    [self addSubview:_myscrollView];

}

-(void)setModel:(TagModel *)model{
    NSLog(@"=======%@",model.selectTagNameArray);
    
    [_noteAddTagView removeFromSuperview];
   _noteAddTagView=[[UIView alloc]initWithFrame:CGRectMake(0, 44*5+10+44, ScreenWidth, 400)];
    _noteAddTagView.backgroundColor=[UIColor whiteColor];
    [_myscrollView addSubview:_noteAddTagView];
    
    _tagVIew=[[UIView alloc]initWithFrame:CGRectMake(0, 15, ScreenWidth, 100)];
    _tagVIew.backgroundColor=[UIColor whiteColor];
    [_noteAddTagView addSubview:_tagVIew];

    UILabel* tag=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, ScreenWidth*70/750,44)];
    tag.text=[NSString stringWithFormat:@"%@", @"标签"];
    tag.font=[UIFont systemFontOfSize:15];
    tag.textColor=[UIColor blackColor];
    [_noteAddTagView addSubview:tag];
    
    _tagLabel=[[UILabel alloc]initWithFrame:CGRectMake(90,  0, 170,44)];
    _tagLabel.text=[NSString stringWithFormat:@"%@", @"请选择标签"];
    _tagLabel.font=[UIFont systemFontOfSize:15];
    _tagLabel.textColor=[UIColor colorWithRed:195/255.0 green:196/255.0 blue:200/255.0 alpha:1.0];
    [_noteAddTagView addSubview:_tagLabel];
    
    UIButton*tagBtn=[[UIButton alloc]initWithFrame:CGRectMake(50, 0, ScreenWidth, 88/2)];
    [tagBtn addTarget:self action:@selector(tagBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_noteAddTagView addSubview:tagBtn];
    UIButton*jiantou = [[UIButton alloc]init];
    jiantou.frame = CGRectMake(ScreenWidth-30,15,8, 13);
    [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
    [_noteAddTagView addSubview:jiantou];
    
    UIImageView *_xianview = [[UIImageView alloc]initWithFrame:CGRectMake(15,44, ScreenWidth-30, 0.5)];
    _xianview.image=[UIImage imageNamed:@"hengxian"];
    [_noteAddTagView addSubview:_xianview];
    
   
    
    if (model.selectTagNameArray.count>0) {
        _tagLabel.text=[NSString stringWithFormat:@"(已选择%ld个)", model.selectTagNameArray.count];
        _tagLabel.textColor=shenhongseColor;
        NSLog(@"%@",model.selectTagNameArray);
        for (int i = 0,j = 0; i <model.selectTagNameArray.count; i++) {
            if (i % 4 == 0) {
                j++;
            }
            
            tagV = [[TagView alloc]initWithFrame:CGRectMake(12.5+(ScreenWidth/4-5)*(i%4), (40*j), ScreenWidth/4-15, 26)];
            tagV.buttonView.titleLabel.textAlignment=NSTextAlignmentCenter;
            tagV.buttonView.titleLabel.font=[UIFont systemFontOfSize:contenfot];
            [tagV.buttonView setTitle: [model.selectTagNameArray objectAtIndex:i]  forState:UIControlStateNormal];
            CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
            CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){209/255.0,170/255.0, 56/255.0, 1.0   });
            tagV.buttonView.backgroundColor=[UIColor colorWithRed:255/255.0 green:255/255.0 blue:234/255.0 alpha:1];
            [tagV.buttonView.layer setBorderColor:colorref2];//边框颜色
            [tagV.buttonView setTitleColor:[UIColor colorWithRed:209/255.0 green:170/255.0 blue:56/255.0 alpha:1.0] forState:0];
            [_tagVIew addSubview:tagV];
            
        }
  _tagVIew.height= tagV.bottom;
    
    }else{
        
        
     _tagVIew.frame=CGRectMake(0, 44, ScreenWidth, 0);
    }
    
    
        UIView*huiseview=[[UIView alloc]initWithFrame:CGRectMake(0,_tagVIew.bottom+10, ScreenWidth, 10)];
        huiseview.backgroundColor=zuiqianhuiColor;
        huiseview.layer.borderWidth = 0.35;
        huiseview.layer.borderColor = qianhuiColor.CGColor;
        [_noteAddTagView addSubview:huiseview];

    
    
    _noteView=[[UITextView alloc]init];
    _noteView.frame=CGRectMake(10, huiseview.bottom+10, ScreenWidth-30, ScreenHeight*185/1334);
    _noteView.backgroundColor = [UIColor whiteColor];//设置它的背景颜色/Users/jiezsoft/Desktop/
    _noteView.scrollEnabled = YES;//是否可以拖动
    _noteView.delegate=self;
    _noteView.font = [UIFont fontWithName:@"Arial" size:15.0];//设置字体名字和字体大小
    _noteView.layer.borderWidth =0;
    CGColorSpaceRef registedSpace11 = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref111 = CGColorCreate(registedSpace11,(CGFloat[]){ 0, 0, 0, 0.3 });
    [_noteView.layer setBorderColor:tureBTncolorref111];//边框颜色
    _noteView.layer.cornerRadius =0;
    [_noteAddTagView addSubview:_noteView];
    
    ima=[[UIImageView alloc]initWithFrame:CGRectMake(10, 8, 51/3, 43/3)];
    ima.image=[UIImage imageNamed:@"_0000_xingxifankui.png"];
    [_noteView addSubview:ima];
    labelNote=[[UILabel alloc]init];
    labelNote.frame =CGRectMake(33, 8, ScreenWidth-60, 15);
    labelNote.text = @"备注";
    labelNote.textColor=qianhuiColor;
    labelNote.numberOfLines=0;
    labelNote.font=[UIFont systemFontOfSize:15];
    labelNote.enabled = NO;//lable必须设置为不可用
    labelNote.backgroundColor = [UIColor clearColor];
    [_noteView addSubview:labelNote];
    
    _noteView.inputAccessoryView = myToolBar;
    _noteAddTagView.frame=CGRectMake(0,44*5+10+44, ScreenWidth, _noteView.bottom);
    [_myscrollView setContentSize:CGSizeMake(ScreenWidth, _noteAddTagView.bottom+300)];
   

}

-(void)tagBtn:(UIButton *)sender{

  [mDelegate tagBtn:@""];
}
-(void)QrCodeBtn:(UIButton *)sender{
    [mDelegate QrCodeBtn:@""];
}
-(void)area:(UIButton *)sender{
    [self OnTapDone];
    [mDelegate area:@""];

}

//账号长度限制
- (void)userNumberLimitTextLength:(UITextField*)textField
{
     [mDelegate cancelLocatePicker:@""];
    if ([textField.text length] > 11)
    {
        textField.text = [textField.text substringToIndex:11];
    }
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{

     [mDelegate cancelLocatePicker:@""];
    if (textField.tag == 5)
    { [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        _myscrollView .frame =CGRectMake(0, -50, ScreenWidth, ScreenHeight);
        [UIView commitAnimations];
    }
    
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    _myscrollView .frame =CGRectMake(0, -150, ScreenWidth, ScreenHeight);
    [UIView commitAnimations];
    return self;
}

-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        [ima removeFromSuperview];
        ima=[[UIImageView alloc]initWithFrame:CGRectMake(10, 8, 51/3, 51/3)];
        ima.image=[UIImage imageNamed:@"_0000_xingxifankui.png"];
        [_noteView addSubview:ima];
        labelNote.text = @"备注";
    }else{
        [ima removeFromSuperview];
        labelNote.text = @"";
    }
}


//回收键盘
-(void)OnTapDone
{
    [_addressTextFied resignFirstResponder];
    [_regionTextField resignFirstResponder];
    [_phoneTextFied resignFirstResponder];
    [_userNameTextFied resignFirstResponder];
    [_noteView resignFirstResponder];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    _myscrollView .frame =CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    [UIView commitAnimations];
    [mDelegate cancelLocatePicker:@""];

}

@end
